from farstyle.models import CSS, JS
from farstyle import settings

from django.contrib.sites.models import Site

import unittest
import os

TEST_CSS = {
    '1.css': {'content': 'CSS 1', 'attrs': {'order': 1}},
    '2.css': {'content': 'CSS 2', 'attrs': {'order': 4}},
    '3.css': {'content': 'CSS 3', 'attrs': {'order': 2, 'media': 'screen'}},
    '4.css': {'content': 'CSS 4', 'attrs': {'order': 3}},
    '5.css': {'content': 'CSS 5', 'attrs': {'order': 4, 'condition': '"IE" in params'}},
    '6.css': {'content': 'CSS 6', 'attrs': {'order': 5}},
}

TEST_JS = {
    '1.js': {'content': 'JS 1', 'attrs': {'order': 1}},
    '2.js': {'content': 'JS 2', 'attrs': {'order': 4}},
    '3.js': {'content': 'JS 3', 'attrs': {'order': 2}},
    '4.js': {'content': 'JS 4', 'attrs': {'order': 3}},
    '5.js': {'content': 'JS 5', 'attrs': {'order': 5}},
}

DEFAULT_SETTINGS = {
    'CSS_FILTERS': [],
    'JS_FILTERS': [],
    'ASSETS_DIR': '#assets#',
}

class ChangeSettings:
    def __init__(self):
        self.default_settings = DEFAULT_SETTINGS.copy()

    def change(self, override=None):
        if override is not None:
            self.default_settings.update(override)
        for setting, default in self.default_settings.items():
            self_s = 'original_%s' % setting
            if hasattr(settings, setting) and not hasattr(self, self_s):
                setattr(self, self_s, getattr(settings, setting))
            if hasattr(settings, setting) or \
               default != DEFAULT_SETTINGS[setting]:
                setattr(settings, setting, default)

    def revert(self):
        for setting in self.default_settings:
            self_s = 'original_%s' % setting
            if hasattr(self, self_s):
                setattr(settings, setting, getattr(self, self_s))
                delattr(self, self_s)

def make_asset(path, name, spec, klass):
    path = os.path.join(path, name)
    f = open(path, 'w')
    f.write(spec['content'])
    f.close()
    obj = klass.objects.create(path=path, **spec['attrs'])

def delete_dir(dir):
    for root, dirs, files in os.walk(dir, topdown=False):
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            os.rmdir(os.path.join(root, name))
    os.rmdir(dir)

class BaseTest(unittest.TestCase):
    css_path = '#css#'
    js_path = '#js#'
    asset_path = '#assets#'

    def setUp(self):
        self.cwd = os.getcwd()
        current_site = Site.objects.get_current()

        os.chdir(settings.MEDIA_ROOT)
        os.mkdir(self.css_path)
        os.mkdir(self.js_path)
        os.mkdir(self.asset_path)

        for name, spec in TEST_CSS.items():
            make_asset(self.css_path, name, spec, CSS)
        [a.sites.add(current_site) for a in list(CSS.objects.all())[:-1]]

        for name, spec in TEST_JS.items():
            make_asset(self.js_path, name, spec, JS)
        [a.sites.add(current_site) for a in list(JS.objects.all())[:-1]]

        self.change_settings = ChangeSettings()
        self.change_settings.change()

    def tearDown(self):
        delete_dir(self.css_path)
        delete_dir(self.js_path)
        delete_dir(self.asset_path)
        self.change_settings.revert()
        os.chdir(self.cwd)
